using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BaseForum.Model;
using WebForum.App_Data;

namespace WebForum
{
    public partial class CreateTopic : System.Web.UI.Page
    {
        private string action;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] == null)
                Common.Show("User must login to create topic!", "Home.aspx");
            action = Request.QueryString["action"] == "new" ? "new" : "edit";
            if (!IsPostBack)
            {
                if (action == "edit")
                {
                    LoadTopic();
                }
            }
        }
        /// <summary>
        /// Load old content of topic
        /// </summary>
        protected void LoadTopic()
        {
            Topic topic = TopicDataSource.GetFull(Common.ParseInt32(Request.QueryString["id"]));
            //Forum forum = ForumDataSource.Get(topic.Forum.Id);
            DropDownListSubForum.SelectedValue = topic.Forum.Id.ToString();
            Forum pForum = ForumDataSource.Get(topic.Forum.ParrentForum.Id);
            DropDownListForum.SelectedValue = pForum.Id.ToString();
            TextBoxTitle.Text = topic.Title;
            TextBoxContent.Text = topic.Content;
        }

        protected void ButtonSubmit_Click(object sender, EventArgs e)
        {
            if (action == "new") //Add new
            {
                Topic topic = new Topic();
                topic.Forum = ForumDataSource.Get(Common.ParseInt32(DropDownListSubForum.SelectedValue));
                topic.User = (User)Session["user"];
                topic.Title = TextBoxTitle.Text;
                topic.Content = TextBoxContent.Text;
                topic.ParrentTopic = null;
                topic.PostDate = new DateTime();
                if (TopicDataSource.Insert(topic))
                    Common.Show("Topic Posted!", "ViewTopic.aspx?id=" + topic.Id);
                else
                    Common.Show("Topic insert faild!", Request.UrlReferrer.ToString());
            }
            else //Edit
            {
                Topic topic = TopicDataSource.Get(Common.ParseInt32(Request.QueryString["id"]));
                topic.Forum = ForumDataSource.Get(Common.ParseInt32(DropDownListSubForum.SelectedValue));
                topic.User = (User)Session["user"];
                topic.Title = TextBoxTitle.Text;
                topic.Content = TextBoxContent.Text;
                topic.ModifiedDate = new DateTime();
                if (TopicDataSource.Update(topic))
                    Common.Show("Topic Modified!", "ViewTopic.aspx?id=" + topic.Id);
                else
                    Common.Show("Topic modify faild!", Request.UrlReferrer.ToString());
            }
        }

        protected void ButtonCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Home.aspx");
        }
    }
}
